import { mapGetters } from 'vuex'
import { exportExcel } from '@/credit/mixin/taskTableProvinceUnion'

export default {
  data() {
    return {
      loading: false,
      tableData: [],
      totalCount: 0,
      exportLoading: false,
      tableName: 'task_list_view_db',
      dateFmt: 'yyyy-MM-dd hh:mm',
      typeParams: {
        'gua_type.eq': 4,
        'channel_type.eq': 0,
        'task_status.eq': 0,
      },
    }
  },
  computed: {
    ...mapGetters(['user', 'userId', 'job', 'org', 'clientId']),
    userParams() {
      return {
        'task_person.eq': this.userId,
      }
    },
  },
  methods: {
    getParams() {
      return {
        ...this.params,
        ...this.typeParams,
        ...this.userParams,
      }
    },
    fetchData(params) {
      return this.$utils.promise(
        this.$api.getData({
          table: this.tableName,
          params,
          order: this.order,
        })
      )
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res = {}] = await this.fetchData(this.getParams())
      this.loading = false

      const data = res.data || {}
      this.totalCount = data.totalCount || 0
      this.tableData = (data.data || []).map(this.dataAdapter)
    },
    async exportExcel(filename) {
      this.exportLoading = true
      exportExcel({
        filename,
        tableColumns: this.tableColumns,
        params: this.getParams(),
        dataAdapter: this.dataAdapter,
        fetchData: this.fetchData,
        onLoaded: () => {
          this.exportLoading = false
        },
      })
    },
  },
}
